#!/usr/bin/env python
#coding=utf-8

import os
import wx
import wx.lib.customtreectrl as CT
import  wx.gizmos   as  gizmos

class CustomTreeCtrl(CT.CustomTreeCtrl):
    def __init__(self, parent, id, pos, size, style):
                    
        CT.CustomTreeCtrl.__init__(self, parent, id, pos, size, style)
        self.Bind(wx.EVT_LEFT_DCLICK, self.onDoubleClick)

    def onDoubleClick(self, event):
        event.Skip(False)


class CustomView:
    def __init__(self,parent,kind=0):
        #self.tree = kind and CustomTreeCtrl(self,parent,id ,pos,size,style) or gizmos.TreeListCtrl(self,parent,id,pos,size,style)
        self.kind = kind
        if self.kind:
            self.tree = CustomTreeCtrl(parent=parent,id=wx.ID_ANY ,pos=wx.Point(0, 0),size=wx.DefaultSize,
                                        style=wx.TR_HIDE_ROOT | wx.TR_HAS_BUTTONS | wx.TR_HAS_VARIABLE_ROW_HEIGHT)
        else:
            self.tree = gizmos.TreeListCtrl(parent=parent,pos=wx.Point(0, 0),size=wx.DefaultSize,
                                            style = wx.TR_DEFAULT_STYLE| wx.TR_FULL_ROW_HIGHLIGHT| wx.TR_HIDE_ROOT|wx.TR_ROW_LINES)
            columTxt = [u"标题",u"字数",u"作者",u"阅读",u"时间"]
            for txt in columTxt:
                self.tree.AddColumn(txt)
            self.tree.SetMainColumn(0)
            self.tree.SetColumnWidth(0, 500)
            self.tree.SetColumnWidth(4, 120)
    def GetTree(self):
        return self.tree
    def SetImageList(self,imgLst):
        self.tree.SetImageList(imgLst)
    def GetId(self):
        return self.tree.GetId()
    def SetBackgroundImage(self,img):
        if self.kind :
            self.tree.SetBackgroundImage(img)
        else:
            pass
    def GetItemDataByEvent(self,event):
        if self.kind:
            item,value =  self.tree.HitTest(event.GetPosition())
            return self.tree.GetPyData(item)
        else:
            return self.tree.GetItemPyData(event.GetItem())
    def Destroy(self):
        self.tree.Destroy()
    def AddRoot(self,txt):
        return self.tree.AddRoot(txt)
    def SetItemPyData(self,item,data):
        if self.kind :
            self.tree.SetPyData(item,data)
        else:
            self.tree.SetItemPyData(item,data)
    def GetItemPyData(self,item):
        if self.kind:
            return self.tree.GetPyData(item)
        else:
            return self.tree.GetItemPyData(item)
    def AppendItem(self,par,title,image,selectedImage,wordCount,author,clickCount,time):
        if self.kind:
            strTemp =  title+" ("+wordCount+") ("+author+" ["+clickCount+"]) "+time
            return self.tree.AppendItem(par,strTemp,image=image,selImage=selectedImage)
        else:
            item = self.tree.AppendItem(parent=par,text = title,image=image,selectedImage=selectedImage)
            self.tree.SetItemText(item,wordCount,1)
            self.tree.SetItemText(item,author,2)
            self.tree.SetItemText(item,clickCount,3)
            self.tree.SetItemText(item,time,4)
            return item
            
    def ExpandAll(self,item):
        if self.kind:
            self.tree.ExpandAll() 
        else:
            self.tree.ExpandAll(item)  